package com.cakeclient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cakeclient.Model.Store;

import java.util.List;

public class ClientStoreListDialogActivity extends Activity {
    /** Called when the activity is first created. */

    private ListItemsAdapter adapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.client_storelist_dialog);

		final ListView list = (ListView) findViewById(R.id.list);
		adapter = new ListItemsAdapter(Store.getAll(getApplicationContext()));
		list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Store.setLocalStore(getApplicationContext(),Store.getAll(getApplicationContext()).get(i));
                Intent intent = new Intent(ClientStoreListDialogActivity.this, MainActivity.class);
                setResult(RESULT_OK, intent);
                finish();
            }
        });


    }



	private class ListItemsAdapter extends ArrayAdapter<Store> {
        List<Store> items;
		public ListItemsAdapter(List<Store> items) {
			super(ClientStoreListDialogActivity.this, android.R.layout.simple_list_item_1, items);
            this.items = items;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;

			LayoutInflater inflater = getLayoutInflater();
			convertView = inflater.inflate(R.layout.dialog_items, null);

			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.text);		

			convertView.setTag(holder);

			// Bind the data efficiently with the holder.
			holder.text.setText( items.get(position).getName() );

			
			return convertView;
		}

		private class ViewHolder {
			TextView text;
		}
	}
}